package simorg.actions;

import java.util.Collection;

import org.dom4j.Document;
import org.dom4j.Element;

import simorg.controller.UserController;
import simorg.domain.User;
import simorg.domain.persistence.UserTable;
import simorg.view.ModelView;
import simorg.view.Page;
import simorg.view.WebSessionContext;

public class UserList extends Action {

	public static final String actionName = "list";
	public UserList() {
		super(actionName);
		this.controllerName = UserController.controllerName;
	}
	
	@Override
	public ModelView checkAccess(WebSessionContext wc) throws Exception {
		return this.checkAdminAccess(wc);
	}
	
	@Override
	public ModelView process(WebSessionContext wc) throws Exception {
		Document xmlDoc = this.getBaseXml(wc);
		
		Element userList = xmlDoc.getRootElement().addElement( "users" );
		Collection<User> users = UserTable.findAll(wc.getDbConnection());
		for ( User user : users) {
			user.buildDomainXml(userList);
		}
		
		return new ModelView(Page.UserList, xmlDoc);
		
	}

}
